package com.app.xinfadi.ui.user;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.app.xinfadi.R;
import com.app.xinfadi.base.BaseActivity;
import com.app.xinfadi.util.Toast;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 找回密码
 *
 * @author WuTengFei <br/>
 * @Function: TODO <br/>
 * @date 2015-5-4 下午4:24:26 <br/>
 */
public class UserFindPwdActivity extends BaseActivity {

    private static final int UPDATE_TEXTVIEW = 0;
    private TextView login_reg_new;
    private EditText et_phone;
    private EditText et_code;
    private String text_phone;
    private String text_code;
    private Button submit;
    private TextView inpunt_pwd;
    private TextView inpunt_pwd_again;
    private String pwd;
    private String pwd2;
    private CheckBox cb_chexk;
    private TextView agree_info;
    private Button sms_code;
    private String TAG = UserFindPwdActivity.class.getSimpleName();
    private String identifying_code;
    private Timer mTimer;
    private int Max_reckon = 60;
    private int recLen;
    private TimerTask mTimerTask;
    private MyHandler mHandler =new MyHandler(this);
//    Handler mHandler = new Handler() {
//
//        @Override
//        public void handleMessage(Message msg) {
//            switch (msg.what) {
//                case UPDATE_TEXTVIEW:
//                    updateTextView();
//                    break;
//                default:
//                    break;
//            }
//        }
//    };
    static class MyHandler extends Handler {
        private final WeakReference<UserFindPwdActivity> outerClass;

        MyHandler(UserFindPwdActivity theClass) {
            outerClass = new WeakReference<UserFindPwdActivity>(theClass);
        }

        @Override
        public void handleMessage(android.os.Message msg) {
            UserFindPwdActivity theClass = outerClass.get();
            switch (msg.what) {
                case UPDATE_TEXTVIEW:
                    theClass.updateTextView();
                    break;
                default:
                    break;
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_findpwd);initToolbar();
        initView();
    }
    private void initToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        TextView title = (TextView) findViewById(R.id.titleTv);
        title.setVisibility(View.VISIBLE);
        title.setText("找回密码");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
    protected void initView() {
        et_phone = (EditText) findViewById(R.id.inpunt_account);
        et_code = (EditText) findViewById(R.id.inpunt_smscode);
        inpunt_pwd = (TextView) findViewById(R.id.inpunt_pwd);
        inpunt_pwd_again = (TextView) findViewById(R.id.inpunt_pwd_again);
        submit = (Button) findViewById(R.id.reg_submit);
        sms_code = (Button) findViewById(R.id.sms_code);

        submit.setOnClickListener(this);
        sms_code.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        text_phone = et_phone.getText().toString().trim();
        text_code = et_code.getText().toString().trim();
        switch (v.getId()) {
            // 提交注册
            case R.id.reg_submit:
                // 判断两次密码是否不为null 并且输入一致
                pwd = inpunt_pwd.getText().toString().trim();
                pwd2 = inpunt_pwd_again.getText().toString().trim();
                // 1. 请求服务器, 验证手机号和验证码是否匹配
                if (text_phone.length() <= 0) {
                    Toast.makeText(this, "请填写手机号码", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(identifying_code)) {
                    Toast.makeText(this, "请获取验证码", Toast.LENGTH_SHORT).show();
                } else if (text_code.length() <= 0) {
                    Toast.makeText(this, "请填写验证码", Toast.LENGTH_SHORT).show();
                } else if (!text_code.equals(identifying_code)) {
                    Toast.makeText(context, "输入的验证码不正确！", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd2)) {
                    Toast.makeText(this, "密码不能为空!", Toast.LENGTH_SHORT).show();
                } else if (!pwd.equals(pwd2)) {
                    Toast.makeText(this, "两次输入密码不相同!", Toast.LENGTH_SHORT).show();
                    // 判断是否勾选了协议
                } else {
                    submitPassword(pwd, text_phone);
                }

                break;
        /*
         * case R.id.regee_agree_info: Toast.makeText(this, "阅读", 0).show();
		 * break;
		 */
            case R.id.sms_code:
                startTimer();// timeTask
                Toast.makeText(this, "发送验证码", Toast.LENGTH_SHORT).show();
                getForgetPasswordSerialNumber();
                break;
            default:
                break;
        }
    }

    private void submitPassword(final String password, final String phone_number) {
        showProgressDialog("正在提交");
        Toast.makeText(context, "密码更改成功！", Toast.LENGTH_SHORT).show();
        finish();
//        UserDao userDao = new UserDao();
//        userDao.forgetPassword(password, phone_number, new Response.Listener<Success>() {
//            @Override
//            public void onResponse(Success success) {
//                if (success.success) {
//                    Toast.makeText(context, "密码更改成功！", Toast.LENGTH_SHORT).show();
//                    finish();
//                } else
//                    Toast.makeText(context, "提交失败！", Toast.LENGTH_SHORT).show();
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError volleyError) {
//                Toast.makeText(context, "提交失败！", Toast.LENGTH_SHORT).show();
//            }
//        });
        dismissProgressDialog();
    }

    /**
     * 获取验证码
     */
    private void getForgetPasswordSerialNumber() {
//        UserDao userDao = new UserDao();
//        userDao.getForgetPasswordSerialNumber(et_phone.getText().toString().trim(), new Response.Listener<SerialNumberBean>() {
//            @Override
//            public void onResponse(SerialNumberBean serialNumberBean) {
//                if (serialNumberBean.success) {
//                    if (serialNumberBean.identifying_code.equals("-1")) {
//                        Toast.makeText(context, "验证码获取错误", Toast.LENGTH_SHORT).show();
//                    } else {
//                        identifying_code = serialNumberBean.identifying_code;
//                        et_phone.setEnabled(false);
//                    }
//                }
//            }
//        });
    }

    private void startTimer() {
        if (mTimer == null) {
            mTimer = new Timer();
        }

        if (mTimerTask == null) {
            recLen = Max_reckon;
            mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    recLen--;
                    Message message = Message.obtain(mHandler, UPDATE_TEXTVIEW);
                    mHandler.sendMessage(message);
                }
            };
        }

        if (mTimer != null && mTimerTask != null)
            mTimer.schedule(mTimerTask, 1000, 1000);

    }

    private void stopTimer() {

        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }

        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }

        recLen = Max_reckon;

    }

    private void updateTextView() {
        if (recLen < 0) {
            stopTimer();
            sms_code.setEnabled(true);
            sms_code.setText("获取验证码");
            stopTimer();
            sms_code.setSelected(false);

        } else {
            sms_code.setText("(" + recLen + ") 获取验证码");
            sms_code.setEnabled(false);
            sms_code.setSelected(true);
        }

    }
}
